package com.mk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mk.base.exception.ApplicationException;
import com.mk.base.exception.StatusCode;
import com.mk.base.service.impl.BaseService;
import com.mk.common.BeanCopier;
import com.mk.common.Config;
import com.mk.mapper.RoleMapper;
import com.mk.model.entity.Role;
import com.mk.model.entity.UserRole;
import com.mk.model.request.RoleRequest;
import com.mk.service.IRoleService;
import com.mk.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 4:14 PM 3/13/18
 */
@Service
public class RoleService extends BaseService<RoleMapper,Role> implements IRoleService {

    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public Role saveRole(RoleRequest request) {
        if(request == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请传入需要添加的角色信息");
        if(StringUtils.isEmpty(request.getName()))
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请传入添加角色的名称");
        Role role = BeanCopier.copy(request,Role.class);
        Date nowDate = new Date();
        role.setCreateTime(nowDate);
        role.setUpdateTime(nowDate);
        if(super.insert(role))
            return role;
        else
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"插入角色信息失败");
    }

    @Override
    public Boolean updateRole(RoleRequest request) {
        if(request == null || request.getId() == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择需要编辑的角色信息");
        Role role = BeanCopier.copy(request,Role.class);
        role.setUpdateTime(new Date());
        return super.updateById(role);
    }

    @Override
    public Boolean deleteRole(Long id) {
        if(id == null)
            throw new ApplicationException(StatusCode.BAD_REQUEST.getCode(),"请选择删除的角色信息");
        Role exist = get(id);
        if(exist == null)
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"删除的角色信息不存在");
        List<UserRole> userRoles = userRoleService.findByRoleId(id);
        if(!CollectionUtils.isEmpty(userRoles))
            throw new ApplicationException(StatusCode.CONFLICT.getCode(),"该条角色信息正在使用，无法删除");
        exist.setUpdateTime(new Date());
        exist.setIsDeleted(Config.ABLE_CONFIG.UNABLE);
        return super.updateById(exist);
    }

    @Override
    public Role get(Long id) {
        return super.selectById(id);
    }

    @Override
    public List<Role> findByUserId(Long userId) {
        return baseMapper.findByUserId(userId,Config.ABLE_CONFIG.ABLE);
    }

    @Override
    public List<Role> findAll() {
        return super.selectList(new EntityWrapper<Role>().eq(Config.ABLE_CONFIG.ABLE_COLUMN,Config.ABLE_CONFIG.ABLE));
    }

    @Override
    public List<Role> getByUsername(String username) {
        return baseMapper.findRoleByUsername(username, Config.ABLE_CONFIG.ABLE);
    }
}
